package com.chuangmei.com.arindoorlocationsystem.base;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;

import com.chuangmei.com.arindoorlocationsystem.R;
import com.chuangmei.com.arindoorlocationsystem.utils.AppActivityManagerUtils;
import com.chuangmei.com.arindoorlocationsystem.utils.LogUtils;

import butterknife.ButterKnife;

/**
 * 项目名称：ARIndoorLocationSystem
 * 类描述：
 * 创建人：Administrator
 * 创建时间：2018/4/4 10:26
 * 修改人：Administrator
 * 修改时间：2018/4/4 10:26
 * 修改备注：
 */

public abstract class BaseActivity extends AppCompatActivity {
    protected boolean actionBar_hide = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        AppActivityManagerUtils.getInstance().addActivity(this);//添加Activity到栈中统一管理

        /*加载布局文件*/
        initLayout();
        setStatusBar();
        ButterKnife.bind(this);
        initView();
        initData();
    }


    private void setStatusBar() {
        if (!actionBar_hide) {
            //设置 paddingTop
            ViewGroup rootView = (ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content);
            rootView.setPadding(0, AppActivityManagerUtils.getInstance().getStatusBarHeight(this), 0, 0);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //5.0 以上直接设置状态栏颜色
                getWindow().setStatusBarColor(getResources().getColor(R.color.common_tv_dark));
            } else {
                //根布局添加占位状态栏
                ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
                View statusBarView = new View(this);
                ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        AppActivityManagerUtils.getInstance().getStatusBarHeight(this));
                statusBarView.setBackgroundColor(getResources().getColor(R.color.common_tv_dark));
                decorView.addView(statusBarView, lp);
            }

        }
    }

    /**
     * 添加Activity转场动画
     *
     * @param intent
     */
    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.anim_slide_in_right, R.anim.anim_slide_out_left);
    }

    /**
     * 添加Activity转场动画
     *
     * @param intent
     * @param requestCode
     */
    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.anim_slide_in_right, R.anim.anim_slide_out_left);
    }

    protected void openActivity(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    /**
     * 添加退出Activity转场动画
     */
    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_right);
    }

    @Override
    protected void onDestroy() {
        AppActivityManagerUtils.getInstance().removeActivity(this);//关闭时移除Activity
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() > 0) {
            getFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }

    protected abstract void initLayout();

    protected abstract void initView();

    protected void initData() {
    }

    public void showToast(String mag) {
        LogUtils.showToast(mag);
    }
}
